package com.ess.user.service.user;

import com.ess.base.service.impl.BaseServiceImpl;
import com.ess.constants.Constants;
import com.ess.user.dto.req.UserReqDto;
import com.ess.user.dto.res.MenuResDto;
import com.ess.user.dto.res.UserResDto;
import com.ess.user.mapper.UserMapper;
import com.ess.user.po.Menu;
import com.ess.user.po.Role;
import com.ess.user.po.User;
import com.ess.user.po.UserPrivilege;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 分页查询用户信息 可以设置排序，模糊查询
     *
     * @param page
     * @param rows
     * @param record
     * @return
     */
    @Override
    public PageInfo<User> queryPageListByCondition(int page, int rows, User record) {
        PageHelper.startPage(page, rows);
        Example example = new Example(User.class);
        example.setOrderByClause(Constants.ORDER_UPDATE_DESC);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted", Constants.STRING_ZERO);
        if (StringUtils.isNotBlank(record.getUserName())) {
            criteria.andLike("userName", "%" + record.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(record.getUserNickname())) {
            criteria.andLike("userNickname", "%" + record.getUserNickname() + "%");
        }
        if (StringUtils.isNotBlank(record.getUserPhone())) {
            criteria.andLike("userPhone", "%" + record.getUserPhone() + "%");
        }
        if (StringUtils.isNotBlank(record.getUserType())) {
            criteria.andEqualTo("userType", record.getUserType());
        }
        if (StringUtils.isNotBlank(record.getUserKind())) {
            criteria.andEqualTo("userKind", record.getUserKind());
        }
        if (StringUtils.isNotBlank(record.getStatus())) {
            criteria.andEqualTo("status", record.getStatus());
        }
        List<User> list = super.mapper.selectByExample(example);
        return new PageInfo<User>(list);
    }


    /**
     * 方法描述：   查询用户信息
     *
     * @param dto
     * @return
     */
    @Override
    public UserResDto queryUserInfo(User dto) {
        UserResDto userInfo = null;
        try {
            UserReqDto res = new UserReqDto();
            res.setSid(dto.getSid());
            res.setUserName(dto.getUserName());
            res.setMenuType(dto.getUserKind());
            List<UserPrivilege> userPrivilegeList = this.userMapper.queryUserPrivilegeList(res);
            if (null != userPrivilegeList && userPrivilegeList.size() > 0) {
                userInfo = new UserResDto();
                userInfo.setUserName(userPrivilegeList.get(0).getUserName());
                //遍历角色信息(去重)
                List<Role> roleInfoList = new ArrayList<Role>();
                //遍历菜单信息(去重)
                List<Menu> menuList = new ArrayList<Menu>();
                Map<String, String> roleNameMap = new HashMap<String, String>();
                for (UserPrivilege privilege : userPrivilegeList) {
                    if (!roleNameMap.containsKey(privilege.getRoleSid())) {
                        roleNameMap.put(privilege.getRoleSid(), privilege.getRoleName());
                        Role roleInfo = new Role();
                        roleInfo.setSid(privilege.getRoleSid());
                        roleInfo.setRoleName(privilege.getRoleName());
                        roleInfoList.add(roleInfo);
                    }
                    Menu menu = new Menu();
                    menu.setSid(privilege.getMenuSid());
                    menu.setMenuName(privilege.getMenuName());
                    menu.setMenuIcon(privilege.getMenuIcon());
                    menu.setMenuLink(privilege.getMenuLink());
                    menu.setMenuLevel(privilege.getMenuLevel());
                    menu.setMenuSort(privilege.getMenuSort());
                    menu.setParentSid(privilege.getParentSid());
                    menu.setPermitMark(privilege.getPermitMark());
                    menu.setIsLeaf(privilege.getIsLeaf());
                    menu.setIsVisible(privilege.getIsVisible());
                    menuList.add(menu);
                }
                userInfo.setRoleList(roleInfoList);
                userInfo.setMenuList(menuList);
            }
        } catch (Exception e) {
            logger.error("查询用户信息异常", e);
        }
        return userInfo;
    }


    /**
     * 方法描述：   查询用户菜单 (用户菜单导航)
     */
    @Override
    public List<MenuResDto> getMenuList(User user) {
        UserResDto userInfo = this.queryUserInfo(user);
        if (null == userInfo) {
            return null;
        }
        List<Menu> menuList = userInfo.getMenuList();
        List<MenuResDto> menuResDtoList1 = new ArrayList<MenuResDto>();//一级菜单
        List<MenuResDto> menuResDtoList2 = new ArrayList<MenuResDto>();//二级菜单
        List<MenuResDto> menuResDtoList3 = new ArrayList<MenuResDto>();//三级菜单
        List<MenuResDto> menuResDtoList4 = new ArrayList<MenuResDto>();//四级菜单
        List<MenuResDto> menuResDtoList5 = new ArrayList<MenuResDto>();//五级菜单
        List<MenuResDto> menuResDtoList6 = new ArrayList<MenuResDto>();//六级菜单
        if (null != menuList) {
            Map<String, String> menuMap = new HashMap<String, String>();
            for (Menu menu : menuList) {
                /*排除隐藏菜单或者已经加过的菜单*/
                if (Constants.STRING_ZERO.equals(menu.getIsVisible()) || menuMap.containsKey(menu.getSid())) {
                    continue;
                }
                menuMap.put(menu.getSid(), menu.getSid());
                MenuResDto menuResDto = new MenuResDto();
                menuResDto.setSid(menu.getSid());
                menuResDto.setMenuName(menu.getMenuName());
                menuResDto.setMenuIcon(menu.getMenuIcon());
                menuResDto.setMenuLink(menu.getMenuLink());
                menuResDto.setMenuLevel(menu.getMenuLevel());
                menuResDto.setMenuSort(menu.getMenuSort());
                menuResDto.setParentSid(menu.getParentSid());
                menuResDto.setPermitMark(menu.getPermitMark());
                menuResDto.setIsLeaf(menu.getIsLeaf());
                menuResDto.setIsVisible(menu.getIsVisible());
                if (Constants.STRING_ONE.equals(menu.getMenuLevel())) {
                    menuResDtoList1.add(menuResDto);
                } else if (Constants.STRING_TWO.equals(menu.getMenuLevel())) {
                    menuResDtoList2.add(menuResDto);
                } else if (Constants.STRING_THREE.equals(menu.getMenuLevel())) {
                    menuResDtoList3.add(menuResDto);
                } else if (Constants.STRING_FOUR.equals(menu.getMenuLevel())) {
                    menuResDtoList4.add(menuResDto);
                } else if (Constants.STRING_FIVE.equals(menu.getMenuLevel())) {
                    menuResDtoList5.add(menuResDto);
                } else if (Constants.STRING_SIX.equals(menu.getMenuLevel())) {
                    menuResDtoList6.add(menuResDto);
                }
            }
        }

        //组装二级菜单
        this.formatMenu(menuResDtoList1, menuResDtoList2);
        //组装三级菜单
        this.formatMenu(menuResDtoList2, menuResDtoList3);
        //组装四级菜单
        this.formatMenu(menuResDtoList3, menuResDtoList4);
        //组装五级菜单
        this.formatMenu(menuResDtoList4, menuResDtoList5);
        //组装六级菜单
        this.formatMenu(menuResDtoList5, menuResDtoList6);

        return menuResDtoList1;
    }

    /*
        组装6级菜单
     */
    private void formatMenu(List<MenuResDto> list1, List<MenuResDto> list2) {
        for (MenuResDto dto1 : list1) {
            for (MenuResDto dto2 : list2) {
                if (dto2.getParentSid().equals(dto1.getSid())) {
                    dto1.getSubMenuList().add(dto2);
                }
            }
            if (dto1.getSubMenuList().size() > 0) {
                dto1.setHasLeaf(Constants.STRING_ONE);//有子菜单
            } else {
                dto1.setHasLeaf(Constants.STRING_ZERO);//无子菜单
            }
        }
    }
}

